#!/usr/bin/bash
# Start dir = $
# it is enough to give the directory of the configuration file
cwd=$PWD
export cwd
uuidShell=$cwd/Uuid.sh

#echo Path is $PATH

if [ $#  -lt 1 ] ; then 
	echo "Insufficient number of input arguments"
	exit 1
fi

if [ -d  tdd/out ] ; then 
	rm -rf tdd/out
fi

#echo "" > tdd/src/data/test.tmp
# Generate new UUID. cog.py must be installed.
#python /bin/cog.py -d -o tdd/src/data/test.py $1
cp -f $1 tdd/src/data/test.py
python CAAUuidGen.py -genIID >> tdd/src/data/test.py
cp -f tdd/src/data/test.py TestCase.py
python CheckTDDSyntax.py
echo "Last python cmd ret code : " $?

# Replace = with colon  since that's is the Freemarker TDL syntax
sed 's/=/:/g' tdd/src/data/test.py > tdd/src/data/test.tdd

#cat TDDAppendix.py >> tdd/src/data/test.py

python tdd/src/data/test.py
if [ $? != 0 ] ; then
	exit 1
fi
#cp -f tdd/src/data/TDDTemplate.tpl tdd/src/data/test.tdd

#sed script uses 'single quotes' instead of "double quotes." Unix shells never expand $variables in single quotes.
#sed -e "s/UuidCPPValue/$uuidCPP/g" -e "s/UuidIIDValue/$uuidIID/g" tdd/src/data/test.tmp > tdd/src/data/test.tdd
#echo "sed -e "s/${i}Value/$uuidCPP/g" -e "s/${i}IIDValue/$uuidIID/g" tdd/src/data/test.tdd > tdd/src/data/test.tmp"
#sed -e "s/${i}Value/$uuidCPP/g" -e "s/${i}IIDValue/$uuidIID/g" tdd/src/data/test.tdd > tdd/src/data/test.tmp
#
#mv -f tdd/src/data/test.tmp tdd/src/data/test.tdd
#exit
 
##Run fmpp via batch file as Windows environment is needed by the fmpp.exe
./Run.bat tdd
if [ $? != 0 ] ; then 
	exit 1
fi

#/cygdrive/c/Raman/code/CodeGen/fmpp_0.9.14/bin/fmpp -C tdd
cd tdd/out

# Run the Generated shell file to call the BitterGerne.sh file
./BitteGerneRun.sh $cwd
cd $cwd
echo $0 Time Taken : $SECONDS

#================================================================================
# Remove blank lines
# Does not work when called from BitteGerne.h
#================================================================================
beautifySHELL=Beautify.sh
echo "#!/bin/bash" > $beautifySHELL
echo "#This is a generated file do not edit" >> $beautifySHELL
src=`find tdd/out/RAMForayWS -type f -name "*Dlg.cpp"`
src="$src "`find tdd/out/RAMForayWS -type f -name "*Cmd.cpp"`

for i in $src
do
	#echo New File  $i
	sedCmd="sed -e '/./,/^$/!d' $i > $i.tmp"
	sed -e '/./,/^$/!d' $i > $i.tmp	# Faster than cat -s $i > $i.tmp
	echo $sedCmd >> $beautifySHELL
	#eval $sedCmd
	mv -f $i.tmp $i
	echo "mv -f $i.tmp $i" >> $beautifySHELL
done
#chmod 755 $beautifySHELL
#bash -v $beautifySHELL

echo $0 Time after blank line removal : $SECONDS
